﻿/*
--------------------------------------
File: SimpleColorDetect.cs
Project: Themaopdracht 7
Author: Thierry Gerritse

Description:
A very simple color based localisation algorithm for blue numberplate's
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Costella;

namespace THO7_Team8
{
    class SimpleColorDetectBlauw : VisionAlgorithm
    {
        public SimpleColorDetectBlauw(String name)
            : base(name)
        {
        }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            //Image for returning only the license plate without
            Bitmap returnImage = new Bitmap(sourceImage);

            //The original array
            int[] rgbValues = MyImage.BmpToArray(returnImage);

            //The modified array
            int size = rgbValues.Length;
            int[] rgbValues2 = new int[size];

            /// blue color
            int margin = 100;
            int minblue = 243 - (margin); int maxblue = 243 + (margin);
            int mingreen = 140 - (margin); int maxgreen = 140 + (margin);
            int minred = 53 - (margin / 3); int maxred = 53 + (margin / 3);

            //Main loop which checks for yellow pixels
            for (int i = 0; i < size; i++)
            {
                int blue = rgbValues[i] & 255;
                int green = (rgbValues[i] >> 8) & 255;
                int red = (rgbValues[i] >> 16) & 255;
                
                //If the current pixel is yellow)
                if (blue <= maxblue &&
                    blue >= minblue &&
                    green <= maxgreen &&
                    green >= mingreen &&
                    red <= maxred &&
                    red >= minred)
                {
                    //If the current pixel is yellow, copy the color from the original image into the modified array
                    rgbValues2[i] = -1;
                }
                else
                {
                    //If the current pixel is not yellow, make it black in the modified image array
                    rgbValues2[i] = -16777216;
                }
            }

            //*** Outcommented until non recursive blob detection is fixed ***//

            //Filters out blobs which are to small
            //GrowingSelection gs = new GrowingSelection(rgbValues2, returnImage.Width, returnImage.Height);
            //gs.findBlobs(-1);
            //gs.filterSmallBlobs(100);
            //gs.getResult().CopyTo(rgbValues2, 0);

            //*** End of outcommented code ***//

            returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
            return returnImage;
        }
    }
}







